<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2025/4/14
  Time: 13:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>批量添加</title>
    <%! ResultSet rs;%>
<%
    String dbName = "student";
    String userName = "root";
    String userPasswd = "123456";
    String url = "jdbc:mysql://172.21.32.100/" + dbName + "?user=" + userName + "&password=" + userPasswd+"&useUnicode=true&characterEncoding=utf-8";
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url);
    String[] stuNos = new String[] {"202203010002","202203010003","202203010004","202203010005"};
    String[] stuNames = new String[] {"赵博","王松","张蕾","王哲"};
    String[] stuSexs = new String[] {"男","男","女","男"};
    String[] stuPolitcalStatus = new String[] {"预备党员","共青团员","预备党员","共青团员"};
    String[] stuBirthdays = new String[] {"2002-11-01","2002-11-02","2002-11-03","2002-11-04"};
    String[] stuIdentityCards = new String[] {"120100200211012711","120100200211022711","120100200211032711","120100200211042711"};
    String[] stuSpeciality = new String[] {"软件技术","视觉传达","信息安全","机械制造"};
    String[] stuAddress = new String[] {"天津市河西区","天津市和平区","天津市南开区","天津市河北区"};
    String[] stuPostcode = new String[] {"300001","300002","300003","300004"};
    String[] stuTelephones = new String[] {"18739077658","19739077658","18735077658","18739037658"};
    String[] stuEmails = new String[] {"18739077658@163.com","19739077658@qq.com","18735077658@126.com","18735037658@163.com"};
    String sql = "INSERT into student(stu_no,stu_name,stu_sex,stu_politicalstatus,stu_birthday,stu_identitycard,stu_speciality,stu_address,stu_postcode,stu_telephone,stu_email)" +
            "VALUES(?,?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement preparedStatement = conn.prepareStatement(sql);
            for (int i = 0 ;i<4; i++){
                preparedStatement.setString(1,stuNos[i]);
                preparedStatement.setString(2,stuNames[i]);
                preparedStatement.setString(3,stuSexs[i]);
                preparedStatement.setString(4,stuPolitcalStatus[i]);
                preparedStatement.setString(5,stuBirthdays[i]);
                preparedStatement.setString(6,stuIdentityCards[i]);
                preparedStatement.setString(7,stuSpeciality[i]);
                preparedStatement.setString(8,stuAddress[i]);
                preparedStatement.setString(9,stuPostcode[i]);
                preparedStatement.setString(10,stuTelephones[i]);
                preparedStatement.setString(11,stuEmails[i]);
            }
            int[] result = preparedStatement.executeBatch();
            if(result.length > 0)
                System.out.print(" <script type='text/javascript'>alert('添加成功');</script>");
            else
                System.out.print(" <script type='text/javascript'>alert('添加失败');</script>");
        sql = "select * from student ";
        preparedStatement = conn.prepareStatement(sql);
        rs = preparedStatement.executeQuery();
        %>

</head>
<body>
<table border="1">
    <th colspan="7" style="font-size: x-large; alignment: center">学生信息表</th>
    <tr style="text-align: center">
        <td>学号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>政治身份</td>
        <td>出生日期</td>
        <td>身份证号</td>
        <td>所在系部</td>
    </tr>
        <%while (rs.next()){ %>
    <tr style="text-align: center">
        <td><%= rs.getString(1)  %></td>
        <td><%= rs.getString(2)  %></td>
        <td><%= rs.getString(3)  %></td>
        <td><%= rs.getString("stu_politicalstatus")  %></td>
        <td><%= rs.getString("stu_birthday")  %></td>
        <td><%= rs.getString("stu_identitycard")  %></td>
        <td><%= rs.getString("stu_speciality")  %></td>
    </tr>
        <% } %>
        <%
    rs.close();
    preparedStatement.close();
    conn.close();
%>
</table>
</body>
</html>
